package logic

import (
	"context"
	"errors"
	"srv/models/mysqlinit"

	"srv/internal/svc"
	"srv/srv"

	"github.com/zeromicro/go-zero/core/logx"
)

type ReleaseLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewReleaseLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ReleaseLogic {
	return &ReleaseLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ReleaseLogic) Release(in *srv.ReleaseRequest) (*srv.ReleaseResponse, error) {
	// todo: add your logic here and delete this line
	u := mysqlinit.User{}
	mysqlinit.DB.Debug().Where("id = ?", in.UserId).Find(&u)

	if u.ID == 0 {
		logx.Error("用户不存在")
		return nil, errors.New("用户不存在")
	}
	if u.Status != 2 {
		logx.Error("当前无发布权限，请先提交资料")
		return nil, errors.New("当前无发布权限，请先提交资料")
	}
	h := mysqlinit.House{}

	h = mysqlinit.House{

		City:       in.City,
		Address:    in.Address,
		HouseCode:  in.HouseCode,
		HouseFloor: in.HouseFloor,
		HouseAcre:  in.HouseAcre,
		HousePrice: float64(in.HousePrice),
		Describe:   in.Describe,
	}
	err := h.CreateHouse()
	if err != nil {
		logx.Error("发布房源失败")
		return nil, errors.New("发布房源失败")
	}

	return &srv.ReleaseResponse{
		HouseId: int64(h.ID),
	}, nil
}
